<?php
class article extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览文章', '1-2');
		$da = $this -> pub -> get_setting('文章', 'Article');
		$this -> load -> view('adminx/wz', $da);
	}

	function add() {
		$this -> pub -> logp('添加文章', '1-2');
		if ($_POST) {
			$da['fl1'] = $this -> input -> post('fl1');
			$da['brand_id'] = $this -> input -> post('brand_id');
			$da['sort'] = $this -> input -> post('sort');
			$da['title'] = $this -> input -> post('title');
			$da['gjz'] = $this -> input -> post('gjz');
			$da['jj'] = $this -> input -> post('jj');
			$da['pl_type'] = $this -> input -> post('pl_type');
			$da['pic'] = $this -> input -> post('pic');
			$da['url'] = $this -> input -> post('url');
			$da['nr'] = $this -> input -> post('nr');
			$da['sy_tj'] = $this -> input -> post('sy_tj');
			$da['pv'] = $this -> input -> post('pv');

			$da['tj_sp'] = $this -> input -> post('tj_sp');
			$da['hdp_xs'] = $this -> input -> post('hdp_xs');

			$da['voice'] = $this -> input -> post('voice');

			$da['xs'] = $this -> input -> post('xs');
			$da['is_wx'] = $this -> input -> post('is_wx');
			$this -> pub -> insert('wz', $da);
			$this -> pub -> logx('添加内容 - ' . $da['title']);
			echo json_encode(array('type' => 1));
		} else {
			$da = $this -> pub -> get_setting('文章', 'Article');
			$q = $this -> db -> get_where('sp_brand');
			$da['brand'] = $q -> result_array();
			$this -> load -> view('adminx/wz_add', $da);
		}
	}

	function up($idx) {
		$this -> pub -> logp('修改文章 - ' . $idx, '1-2');
		if ($_POST) {
			$da['mx']['fl1'] = $this -> input -> post('fl1');
			$da['mx']['brand_id'] = $this -> input -> post('brand_id');
			$da['mx']['sort'] = $this -> input -> post('sort');
			$da['mx']['title'] = $this -> input -> post('title');
			$da['mx']['gjz'] = $this -> input -> post('gjz');
			$da['mx']['jj'] = $this -> input -> post('jj');
			$da['mx']['pic'] = $this -> input -> post('pic');
			$da['mx']['url'] = $this -> input -> post('url');
			$da['mx']['nr'] = $this -> input -> post('nr');
			$da['mx']['sy_tj'] = $this -> input -> post('sy_tj');
			$da['mx']['pv'] = $this -> input -> post('pv');

			$da['mx']['tj_sp'] = $this -> input -> post('tj_sp');

			$da['mx']['xs'] = $this -> input -> post('xs');

			$da['mx']['pl_type'] = $this -> input -> post('pl_type');
			$da['mx']['is_wx'] = $this -> input -> post('is_wx');

			$da['mx']['hdp_xs'] = $this -> input -> post('hdp_xs');

			$da['mx']['voice'] = $this -> input -> post('voice');

			$this -> db -> update('wz', $da['mx'], array('wz_id' => $idx));
			$da['msg'] = '修改内容成功';
			$da['title'] = '修改内容';
			$da['index'] = '6';
			echo json_encode(array('type' => 1));
		} else {
			$q = $this -> db -> get_where('wz', array('wz_id' => $idx));
			if ($q -> num_rows() > 0) {
				$da['mx'] = $q -> row_array(0);
				$da['msg'] = '';
				$da['title'] = '修改内容';
				$da['index'] = '6';
				$q = $this -> db -> get_where('sp_brand');
				$da['brand'] = $q -> result_array();
				$this -> load -> view('adminx/wz_up', $da);
			} else {
				show_error('您的请求不存在！！');
			}
		}
	}

	function del() {
		$this -> pub -> logp('删除文章 - ' . $this -> input -> post('idx'), '1-2');
		$q = $this -> db -> get_where('wz', array('wz_id' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$this -> db -> update('wz', array('wz_del' => 2), array('wz_id' => $this -> input -> post('idx')));
			$da['type'] = 1;
			$this -> pub -> logx('删除内容 - ' . $this -> input -> post('idx'));
		} else {
			$da['type'] = 2;
		}
		echo json_encode($da);
	}

	function get() {
		$this -> pub -> logp('获取文章信息', '1-2');
		if ($this -> input -> post('key')) {
			$this -> db -> where('wz_del', 1);
			$this -> db -> select('count(*)');

			$this -> db -> like('title', $this -> input -> post('key'));
			$q = $this -> db -> get_where('wz');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			$this -> db -> like('title', $this -> input -> post('key'));
		} else {
			$this -> db -> where('wz_del', 1);
			$this -> db -> select('count(*)');
			$q = $this -> db -> get_where('wz');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('sort', 'asc');
		$this -> db -> where('wz_del', 1);
		$q = $this -> db -> get_where('wz');
		$da['content'] = '';
		$sy_tj[1] = '否';
		$sy_tj[2] = '是';

		$xs[1] = '是';
		$xs[2] = '否';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['wz_id'] . '</td>';
			$da['content'] .= '<td>' . $v['title'] . '</td>';
			$da['content'] .= '<td>' . $v['fl1'] . '</td>';
			$da['content'] .= '<td>' . $v['sort'] . '</td>';
			$da['content'] .= '<td>' . $xs[$v['xs']] . '</td>';
			$da['content'] .= '<td>' . $sy_tj[$v['sy_tj']] . '</td>';
			$da['content'] .= '<td><a href="/adminx/yl#/article/d/' . $v['wz_id'] . '" class="btn default"><i class="fa fa-cloud"></i> 预览</a> <button onclick="alert_ts(\'文章原链接是：' . site_url('article/d/' . $v['wz_id']) . '\')" type="button" class="btn btn-info"><i class="fa fa-link"></i>链接</button> <button onclick="get_ewm(\'' . $v['wz_id'] . '\',\'' . $v['title'] . '\',3)" type="button" class="btn default blue"><i class="fa fa-qrcode"></i>二维码</button> <a href="/iadmin/article/up/' . $v['wz_id'] . '" type="button" class="btn default green"><i class="fa fa-edit"></i> 修改</a>  <button onclick="del(\'' . $v['wz_id'] . '\',\'' . $v['title'] . '\')" type="button" class="btn default red"><i class="fa fa-trash-o"></i> 删除</button></td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>